package com.loopy.cookie;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;

public class ReadCookieByNameServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cookie[] cookies = request.getCookies();
        Cookie username = CookieUtils.readCookieByName("username", cookies);
        if(null!=username){
            System.out.println("cookieName="+username.getName()+"   cookieValue="+username.getValue());
        }else {
            System.out.println("没有找到该cookie");
        }

        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.print("<h1>根据名字读取cookie任务完成</h1>");
        writer.flush();
        writer.close();
    }
}
